import { createAppSlice } from "@/lib/createAppSlice"
import { Team } from "@/type/team"
import { PayloadAction } from "@reduxjs/toolkit"

const initialState: { team: Team, leaveTeam: Team[] } = {
    team: {
        team_name: "",
        team_id: null,
        emoji: 0x1F680
    },
    leaveTeam: []
}

export const teamSlice = createAppSlice({
    name: "team",
    initialState,
    reducers: (create) => ({
        setTeamData: create.reducer((state, action: PayloadAction<Team>) => {            
            state.team = action.payload
        }),
        updateTeamData: create.reducer((state, action: PayloadAction<Team>) => {
            state.team = action.payload
        }),
        setLeaveTeamData: create.reducer((state, action: PayloadAction<Team[]>) => {
            state.leaveTeam = action.payload
        }),
        updateLeaveTeamData: create.reducer((state, action: PayloadAction<Team>) => {
            state.leaveTeam.push(action.payload)
        }),
    }),
    selectors: {
        selectTeamDetail: (team) => team.team,
        selectTeamLeaveDetail: (team) => team,
    }
})

export const { setTeamData, updateTeamData, setLeaveTeamData, updateLeaveTeamData } = teamSlice.actions

export const { selectTeamDetail, selectTeamLeaveDetail } = teamSlice.selectors
